package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.reporsitory.ITravelSearchRepository;
import cn.wolfcode.luowowo.search.service.ITravelSearchService;
import cn.wolfcode.luowowo.search.templates.TravelTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

@Service
public class TravelSearchServiceImpl implements ITravelSearchService{
    @Autowired
    private ITravelSearchRepository dao;
    @Autowired
    private ElasticsearchTemplate template;
    @Autowired
    private TransportClient client;
    //创建索引
    public void createIndex() {
        template.createIndex(TravelTemplate.class);
        template.putMapping(TravelTemplate.class);
    }

    public void initData() {
        
    }

    public void save(TravelTemplate tt) {
        dao.save(tt);
    }

    public Page<TravelTemplate> query(SearchQueryObject qo) {
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        if(qo.getDestId() != -1){
            query.must(QueryBuilders.termQuery("destId",qo.getDestId()));
        }
        return dao.search(query,qo.getPageableWithoutSort());
    }
}
